package com.findsing.jetty.websocket.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.findsing.jetty.websocket.async.AsyncProcesser;
import com.findsing.jetty.websocket.model.Credential;

@Controller
@EnableWebMvc
@RequestMapping("/")
public class LoginConroller {

	@Autowired
	AsyncProcesser asyncProcesser;
	
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public @ResponseBody String hello() 
	{
		return "Welcome Home!";
 	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@Consumes({ MediaType.APPLICATION_JSON_VALUE })
	@Produces({ MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody void login(@RequestBody Credential credential, @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse) 
	{
		HttpSession session = servletRequest.getSession(true);
		session.setAttribute("userName", credential.getUserName());

		asyncProcesser.process(servletRequest.startAsync(servletRequest, servletResponse));
 	}
	
	/*@RequestMapping(value = "/login_s", method = RequestMethod.POST)
	@Consumes({ MediaType.TEXT_PLAIN_VALUE })
	@Produces({ MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody Message login(@RequestBody String credential, @Context HttpServletRequest servlerRequest) 
	{
		HttpSession session = servlerRequest.getSession(false);
		servlerRequest.getCookies();
		
		String userName = (String) session.getAttribute("userName");
		
		Message message = new Message();
		message.setMessageName(userName);
		return message;
 	}*/
}
